<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="版块名称" prop="title">
				<el-input v-model="form.title" placeholder="版块名称" maxlength="48" clearable></el-input>
			</el-form-item>
			<el-form-item label="版块类型" prop="discussType">
				<sc-select v-model="form.discussType" :params="{code:'discussType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
			</el-form-item>
			<el-form-item label="版主" prop="edition">
				<select-user-dialog v-model:accountId="form.edition" :mode="false"/>
			</el-form-item>
			<el-form-item label="开放人员" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="开放部门">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="开放职务">
				<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="是否审批" prop="needApproval">
				<el-switch v-model="form.needApproval" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="查看权限" prop="readRole">
				<el-radio-group v-model="form.readRole">
					<el-radio-button label="1">发布人</el-radio-button>
					<el-radio-button label="2">所有人</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="版块说明">
				<el-input type="textarea" :rows="2" maxlength="180" placeholder="版块说明" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";

export default {
	components: {
		selectUserDialog,
		selectDeptDialog,
		selectUserLevelDialog
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增版块',
				edit: '编辑版块'
			},
			form: {
				recordId: "",
				title: "",
				sortNo: "",
				discussType: "",
				edition: "",
				userRole: "",
				deptRole: "",
				levelRole: "",
				needApproval: "0",
				readRole: "2",
				remark: "",
			},
			rules: {
				title: [
					{required: true, message: '讨论区名称不能为空'}
				],
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if (this.mode == "add") {
						this.isSaveing = true;
						res = await this.$API.info.discuss.insertDiscuss.post(this.form);
						this.isSaveing = false;
					} else if (this.mode == "edit") {
						this.isSaveing = true;
						res = await this.$API.info.discuss.updateDiscuss.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.sortNo = data.sortNo
			this.form.discussType = data.discussType
			this.form.edition = data.edition
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.needApproval = data.needApproval
			this.form.readRole = data.readRole
			this.form.remark = data.remark
		}
	}
}
</script>

<style>
</style>
